package com.newland.mesdk.demo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.newland.mesdk.demo.main.R;
import com.newland.mesdk.demo.device.BlueToothDeviceContext;

public class OptionsDialog extends Dialog implements OnItemClickListener {
	private Context context;
	private OnOptionsClickedListener listener;
	private OnBondClickedListener bondlistener;
	private ListView lvOptions, lvBond;
	private List<BlueToothDeviceContext> blueToothEntities;
	private BlueToothAdapter blueToothAdapter;
	private BlueToothBondAdapter blueToothBondAdapter;
	private List<BluetoothDevice> bonddevices;
	private ListButtonClickListener buttonlistener;

	public interface OnOptionsClickedListener {
		public void onOptionsClicked(View view, int position);
	}

	public interface OnBondClickedListener {
		public void onBondClicked(View view, int position);
	}

	public OptionsDialog(Context context, OnOptionsClickedListener listener, List<BlueToothDeviceContext> blueToothEntities,
			List<BluetoothDevice> bonddevices, OnBondClickedListener bondlistener, ListButtonClickListener buttonlistener) {
		super(context, R.style.options_dialog);
		this.listener = listener;
		this.bondlistener = bondlistener;
		this.blueToothEntities = blueToothEntities;
		this.bonddevices = bonddevices;
		this.buttonlistener = buttonlistener;
		this.context = context;
		init();
	}

	private void init() {
		blueToothEntities = new ArrayList<BlueToothDeviceContext>();
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.options_dialog_layout, null);
		lvOptions = (ListView) view.findViewById(R.id.lv_options);
		lvBond = (ListView) view.findViewById(R.id.lv_bond);
		blueToothAdapter = new BlueToothAdapter(context, blueToothEntities);
		blueToothBondAdapter = new BlueToothBondAdapter(context, bonddevices);
		lvOptions.setAdapter(blueToothAdapter);
		lvBond.setAdapter(blueToothBondAdapter);
		lvBond.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (bondlistener != null) {
					bondlistener.onBondClicked(view, position);
				}
				dismiss();
			}
		});
		lvOptions.setOnItemClickListener(this);
		setCancelable(false);
		setContentView(view);
		setAttribute();
		setAttributesY(0.7f);
		blueToothBondAdapter.setListButtonClickListener(buttonlistener);
	}

	public void setGravity(int gravity) {
		Window dialogWindow = getWindow();
		WindowManager m = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
		dialogWindow.setGravity(gravity);
	}

	private void setAttribute() {
		Window dialogWindow = getWindow();
		WindowManager m = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
		p.width = (int) (Math.min(d.getWidth(), d.getHeight()) * 0.9); // 宽度设置为屏幕的0.65
		dialogWindow.setAttributes(p);
	}

	public void setAttributesY(float sx) {
		DisplayMetrics dm2 = context.getResources().getDisplayMetrics();
		Window dialogWindow = this.getWindow();
		WindowManager.LayoutParams p = dialogWindow.getAttributes(); //
		p.height = (int) (dm2.heightPixels * sx);
		dialogWindow.setAttributes(p);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (listener != null) {
			listener.onOptionsClicked(arg1, arg2);
		}
		dismiss();
	}

	/**
	 * 刷新列表中的数据
	 * 
	 * @param tempDevice
	 */
	public void refreshList(BlueToothDeviceContext tempDevice) {
		this.blueToothEntities.add(tempDevice);
		blueToothAdapter.notifyDataSetChanged();
	}

	/**
	 * 刷新列表中的数据
	 * 
	 * @param device
	 */
	public void refreshList(BluetoothDevice device) {
		this.bonddevices.remove(device);
		blueToothAdapter.notifyDataSetChanged();
	}
}
